﻿using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[DisallowMultipleComponent]
public class NetworkObject : NetworkBehaviour
{
    [SyncVar]
    public int levelInstanceIndex = -1;
    [SyncVar]
    private Vector3 syncEulerAngles;

    private void Awake()
    {
        base.name = base.name.Replace("(Clone)", string.Empty);
        if (NetworkUtility.isNetworkActive && NetworkUtility.isServer)
        {
            this.NetworksyncEulerAngles = base.transform.eulerAngles;
            NetworkUtility.Spawn(base.gameObject);
        }
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        if (initialState)
        {
            this.syncEulerAngles = reader.ReadVector3();
            this.levelInstanceIndex = (int) reader.ReadPackedUInt32();
        }
        else
        {
            int num = (int) reader.ReadPackedUInt32();
            if ((num & 1) != 0)
            {
                this.syncEulerAngles = reader.ReadVector3();
            }
            if ((num & 2) != 0)
            {
                this.levelInstanceIndex = (int) reader.ReadPackedUInt32();
            }
        }
    }

    private void OnDestroy()
    {
        if (NetworkUtility.isNetworkActive && (NetworkUtility.isServer && base.isActiveAndEnabled))
        {
            NetworkUtility.Destroy(base.gameObject);
        }
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        if (forceAll)
        {
            writer.Write(this.syncEulerAngles);
            writer.WritePackedUInt32((uint) this.levelInstanceIndex);
            return true;
        }
        bool flag = false;
        if ((base.syncVarDirtyBits & 1) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.Write(this.syncEulerAngles);
        }
        if ((base.syncVarDirtyBits & 2) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.WritePackedUInt32((uint) this.levelInstanceIndex);
        }
        if (!flag)
        {
            writer.WritePackedUInt32(base.syncVarDirtyBits);
        }
        return flag;
    }

    public override void OnStartClient()
    {
        base.OnStartClient();
        if ((!NetworkUtility.isServer && (this.levelInstanceIndex != -1)) && (Level.current != null))
        {
            Instance instance = Level.current.GetInstance(this.levelInstanceIndex);
            if (instance != null)
            {
                instance.DeserializeComponents(base.gameObject);
            }
        }
        base.transform.eulerAngles = this.syncEulerAngles;
    }

    private void UNetVersion()
    {
    }

    public int NetworklevelInstanceIndex
    {
        get
        {
            return this.levelInstanceIndex;
        }
        [param: In]
        set
        {
            base.SetSyncVar<int>(value, ref this.levelInstanceIndex, 2);
        }
    }

    public Vector3 NetworksyncEulerAngles
    {
        get
        {
            return this.syncEulerAngles;
        }
        [param: In]
        set
        {
            base.SetSyncVar<Vector3>(value, ref this.syncEulerAngles, 1);
        }
    }
}

